{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import time\n",
    "from models import DVNNet, CancelProbModel, GridModel\n",
    "import numpy as np\n",
    "import pickle\n",
    "import KM as KM_algo\n",
    "KM_algo = KM_algo.Munkres().compute\n",
    "from common import find_grid_idx, extract_time_feat, min_gps, max_gps, real_distance, block_number, cuda\n",
    "delta_gps = max_gps - min_gps\n",
    "blocks = block_number[0] * block_number[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "GAMMA = 0.95\n",
    "TICK = 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelfolder = './'\n",
    "dvnnet = DVNNet()\n",
    "dvnnet.load_state_dict(torch.load(modelfolder + 'DVNNet.pt')['model'])\n",
    "cancelProbModel = CancelProbModel()\n",
    "cancelProbModel.load_state_dict(torch.load(modelfolder + 'cancelProbModel.pt')['model'])\n",
    "gridModel = GridModel()\n",
    "gridModel.load_state_dict(torch.load(modelfolder + 'gridModel.pt')['model'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pkl_folder = 'data/pkl/'\n",
    "rd_data = pickle.load(open(pkl_folder + 'grid_order_reward.pkl', 'rb'))\n",
    "meanstd = {'order': [1.3818544802263453, 2.0466071372530115], 'reward': [0.003739948797879627, 0.000964668315987685]}\n",
    "for i in meanstd.keys():\n",
    "    n = meanstd[i]\n",
    "    if i == 'order':\n",
    "        rd_data[i] = np.log(rd_data[i] + 1)\n",
    "    r = rd_data[i]\n",
    "    r -= n[0]\n",
    "    r /= n[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def meanstd(arr, mean, std):\n",
    "    return (arr.astype(float) - mean) / std\n",
    "cancel_eta = [1327.0779045105526, 847.6405218280669]\n",
    "cancel_reward = [4.182467829036552, 2.826104770240745]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def obs2value(obs):\n",
    "    with torch.no_grad():\n",
    "        f_obs = {}\n",
    "        for key in obs[0].keys():\n",
    "            f_obs[key] = np.stack([np.array(x[key]) for x in obs])\n",
    "        \n",
    "        hour = torch.tensor([time.localtime(x).tm_hour for x in f_obs['timestamp']])\n",
    "        dist_pos = (f_obs['order_driver_distance'] / 200).astype(int)\n",
    "        dist_pos[dist_pos > 9] = 9\n",
    "        startpos = meanstd(f_obs['order_start_location'], min_gps, delta_gps)\n",
    "        endpos = meanstd(f_obs['order_finish_location'], min_gps, delta_gps)\n",
    "        dist = ((np.abs(endpos - startpos) * real_distance) ** 2).sum(axis=1) ** 0.5\n",
    "        eta = dist / 3 # use order_finish_timestamp ?\n",
    "        args = [startpos, endpos, hour, meanstd(f_obs['reward_units'], *cancel_reward).astype('float32'), meanstd(eta, *cancel_eta).astype('float32')]\n",
    "        args = [torch.tensor(x) for x in args]\n",
    "        cancel = cancelProbModel(*args).numpy()\n",
    "        cancel = np.choose(dist_pos, cancel.T)\n",
    "        #print(args, dist_pos, cancel)\n",
    "        order, reward = gridModel(torch.tensor(startpos), torch.tensor(hour))\n",
    "        #print(startpos, hour, order, reward)\n",
    "        args = [startpos[:,0], startpos[:,1], hour, order, reward]\n",
    "        args = [torch.tensor(x) for x in args]\n",
    "        vf = dvnnet(*args)\n",
    "        #print(args, vf)\n",
    "        for o, c, v, e in zip(obs, cancel, vf, eta):\n",
    "            o['cancel_prob'] = c\n",
    "            o['value'] = v.item()\n",
    "            o['eta'] = e.item()\n",
    "    return obs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [(2)pos, (,)hour] * x\n",
    "def calc_v(data):\n",
    "    data = list(zip(*data))\n",
    "    pos, hour = [torch.tensor(x) for x in data]\n",
    "    with torch.no_grad():\n",
    "        order, reward = gridModel(pos, hour)\n",
    "        vf = dvnnet(pos[:,0], pos[:,1], hour, order, reward)\n",
    "        return vf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def KM(obs):\n",
    "    driver_id = []\n",
    "    order_id = []\n",
    "    driver_data = []\n",
    "    dset = set()\n",
    "    oset = set()\n",
    "    for o in obs:\n",
    "        did = o['driver_id']\n",
    "        if did not in dset:\n",
    "            dset.add(did)\n",
    "            driver_id.append(did)\n",
    "            order_id.append(-1)\n",
    "            driver_data.append([o['order_start_location'], time.localtime(o['timestamp']).tm_hour])\n",
    "    for o in obs:\n",
    "        oid = o['order_id']\n",
    "        if oid not in oset:\n",
    "            oset.add(oid)\n",
    "            order_id.append(oid)\n",
    "    driver_v = calc_v(driver_data)\n",
    "    edge = np.zeros((len(driver_id), len(order_id)), dtype='float')\n",
    "    edge[:] = -1000\n",
    "    for i in range(len(driver_id)):\n",
    "        edge[i][i] = driver_v[i] * GAMMA\n",
    "    for o in obs:\n",
    "        di = driver_id.index(o['driver_id'])\n",
    "        oi = order_id.index(o['order_id'])\n",
    "        t = o['eta'] + o['pick_up_eta']\n",
    "        t = int(t / TICK) + 1\n",
    "        gt = GAMMA ** t\n",
    "        p = o['cancel_prob']\n",
    "        edge[di][oi] = (o['reward_units'] * (1 - gt) / (1 - GAMMA) + gt * o['value']) * (1 - p) + p * driver_v[di] * GAMMA\n",
    "    res = []\n",
    "    for di, oi in KM_algo(-edge):\n",
    "        if order_id[oi] != -1:\n",
    "            res.append({'order_id': order_id[oi], 'driver_id': driver_id[di]})\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def geodis(a, b):\n",
    "    return ((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** 0.5\n",
    "\n",
    "class Agent(object):\n",
    "    \"\"\" Agent for dispatching and reposition \"\"\"\n",
    "\n",
    "    def __init__(self):\n",
    "        \"\"\" Load your trained model and initialize the parameters \"\"\"\n",
    "        pass\n",
    "\n",
    "    def dispatch(self, dispatch_observ):\n",
    "        \"\"\" Compute the assignment between drivers and passengers at each time step\n",
    "        :param dispatch_observ: a list of dict, the key in the dict includes:\n",
    "            order_id, int\n",
    "            driver_id, int\n",
    "            order_driver_distance, float\n",
    "            order_start_location, a list as [lng, lat], float\n",
    "            order_finish_location, a list as [lng, lat], float\n",
    "            driver_location, a list as [lng, lat], float\n",
    "            timestamp, int\n",
    "            order_finish_timestamp, int\n",
    "            day_of_week, int\n",
    "            reward_units, float\n",
    "            pick_up_eta, float\n",
    "\n",
    "        :return: a list of dict, the key in the dict includes:\n",
    "            order_id and driver_id, the pair indicating the assignment\n",
    "        \"\"\"\n",
    "        res = KM(dispatch_observ)\n",
    "        return res\n",
    "\n",
    "    def reposition(self, repo_observ):\n",
    "        \"\"\" Compute the reposition action for the given drivers\n",
    "        :param repo_observ: a dict, the key in the dict includes:\n",
    "            timestamp: int\n",
    "            driver_info: a list of dict, the key in the dict includes:\n",
    "                driver_id: driver_id of the idle driver in the treatment group, int\n",
    "                grid_id: id of the grid the driver is located at, str\n",
    "            day_of_week: int\n",
    "\n",
    "        :return: a list of dict, the key in the dict includes:\n",
    "            driver_id: corresponding to the driver_id in the od_list\n",
    "            destination: id of the grid the driver is repositioned to, str\n",
    "        \"\"\"\n",
    "        repo_action = []\n",
    "        for driver in repo_observ['driver_info']:\n",
    "            # the default reposition is to let drivers stay where they are\n",
    "            repo_action.append({'driver_id': driver['driver_id'], 'destination': driver['grid_id']})\n",
    "        return repo_action\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'cancel_prob': 0.40057707, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 1126.2238477454885, 'driver_location': [104.17223539384607, 30.6485633653061], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 375.40794924849615, 'driver_id': 36, 'day_of_week': 2}, {'cancel_prob': 0.40057707, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 1053.7547898479709, 'driver_location': [104.1622195095486, 30.662794596354168], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 351.2515966159903, 'driver_id': 208, 'day_of_week': 2}, {'cancel_prob': 0.40057707, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 1138.6571871299093, 'driver_location': [104.160415, 30.656928], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 379.55239570996974, 'driver_id': 1015, 'day_of_week': 2}, {'cancel_prob': 0.4969803, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 1935.2636459261153, 'driver_location': [104.16251256808364, 30.673970411552567], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 645.0878819753717, 'driver_id': 1244, 'day_of_week': 2}, {'cancel_prob': 0.43278942, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 1324.83184991022, 'driver_location': [104.18570855034723, 30.66096164279514], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 441.61061663673996, 'driver_id': 1758, 'day_of_week': 2}, {'cancel_prob': 0.36817744, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 969.4801721971353, 'driver_location': [104.18204833984375, 30.65693332248264], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 323.1600573990451, 'driver_id': 4285, 'day_of_week': 2}, {'cancel_prob': 0.43278942, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 1228.5477420121815, 'driver_location': [104.17724745008681, 30.64855984157986], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 409.5159140040605, 'driver_id': 6133, 'day_of_week': 2}, {'cancel_prob': 0.34985563, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 654.4543412270853, 'driver_location': [104.17593375824893, 30.66356441259522], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 218.15144707569507, 'driver_id': 6844, 'day_of_week': 2}, {'cancel_prob': 0.34985563, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 726.0510402552777, 'driver_location': [104.17337917751736, 30.652246907552083], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 242.0170134184259, 'driver_id': 7510, 'day_of_week': 2}, {'cancel_prob': 0.36817744, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 821.2972562779313, 'driver_location': [104.17636528862847, 30.652264539930556], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 273.76575209264377, 'driver_id': 7700, 'day_of_week': 2}, {'cancel_prob': 0.36817744, 'value': 2.9609832763671875, 'order_id': 0, 'eta': 3137.4133920992626, 'timestamp': 1488330000, 'order_start_location': [104.17213000000001, 30.65868], 'order_finish_timestamp': 1488335000, 'reward_units': 2.620967741935484, 'order_driver_distance': 825.2737189392166, 'driver_location': [104.16680640835654, 30.65285006509489], 'order_finish_location': [104.07704, 30.68109], 'pick_up_eta': 275.09123964640554, 'driver_id': 7899, 'day_of_week': 2}, {'cancel_prob': 0.4483242, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1439.1476028728016, 'driver_location': [104.1622195095486, 30.662794596354168], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 479.7158676242672, 'driver_id': 208, 'day_of_week': 2}, {'cancel_prob': 0.480994, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1817.8173728839545, 'driver_location': [104.17997381743818, 30.686217053660336], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 605.9391242946515, 'driver_id': 708, 'day_of_week': 2}, {'cancel_prob': 0.387403, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1171.0770650337058, 'driver_location': [104.16251256808364, 30.673970411552567], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 390.3590216779019, 'driver_id': 1244, 'day_of_week': 2}, {'cancel_prob': 0.480994, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1874.2759095443903, 'driver_location': [104.16734130859375, 30.68647216796875], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 624.7586365147968, 'driver_id': 1310, 'day_of_week': 2}, {'cancel_prob': 0.48099393, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1895.4101246531948, 'driver_location': [104.16728949012791, 30.68665808893226], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 631.8033748843983, 'driver_id': 1684, 'day_of_week': 2}, {'cancel_prob': 0.44832426, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1549.0436728869656, 'driver_location': [104.18570855034723, 30.66096164279514], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 516.3478909623219, 'driver_id': 1758, 'day_of_week': 2}, {'cancel_prob': 0.45806426, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1725.152898021458, 'driver_location': [104.17261962890625, 30.68612277560764], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 575.0509660071526, 'driver_id': 2065, 'day_of_week': 2}, {'cancel_prob': 0.45806426, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1731.5226281126777, 'driver_location': [104.1697714673517, 30.685776089926435], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 577.1742093708925, 'driver_id': 2738, 'day_of_week': 2}, {'cancel_prob': 0.4483242, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1568.392336314236, 'driver_location': [104.16472561451413, 30.682216094561085], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 522.7974454380786, 'driver_id': 4178, 'day_of_week': 2}, {'cancel_prob': 0.480994, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1904.7210297829317, 'driver_location': [104.16724962643582, 30.68673459605026], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 634.9070099276439, 'driver_id': 4226, 'day_of_week': 2}, {'cancel_prob': 0.45806426, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1707.8128660040907, 'driver_location': [104.18204833984375, 30.65693332248264], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 569.2709553346969, 'driver_id': 4285, 'day_of_week': 2}, {'cancel_prob': 0.480994, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1823.2114885345427, 'driver_location': [104.17995406830457, 30.68627344078293], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 607.7371628448476, 'driver_id': 6448, 'day_of_week': 2}, {'cancel_prob': 0.45806426, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1711.2011947497763, 'driver_location': [104.16735106195891, 30.684903441519857], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 570.4003982499254, 'driver_id': 6670, 'day_of_week': 2}, {'cancel_prob': 0.45806426, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1716.1573286030734, 'driver_location': [104.18170716467857, 30.684651419281284], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 572.0524428676912, 'driver_id': 6739, 'day_of_week': 2}, {'cancel_prob': 0.35595992, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 810.7140809493975, 'driver_location': [104.17593375824893, 30.66356441259522], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 270.2380269831325, 'driver_id': 6844, 'day_of_week': 2}, {'cancel_prob': 0.480994, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1986.3777212837872, 'driver_location': [104.15808051215278, 30.68198784722222], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 662.1259070945957, 'driver_id': 7581, 'day_of_week': 2}, {'cancel_prob': 0.4483242, 'value': 2.9609832763671875, 'order_id': 1, 'eta': 3647.016164328554, 'timestamp': 1488330000, 'order_start_location': [104.17413, 30.67068], 'order_finish_timestamp': 1488335300, 'reward_units': 3.4274193548387095, 'order_driver_distance': 1531.9470260385963, 'driver_location': [104.16552323039143, 30.682281290577755], 'order_finish_location': [104.06004, 30.66109], 'pick_up_eta': 510.6490086795321, 'driver_id': 7848, 'day_of_week': 2}]\n",
      "[{'order_id': 1, 'driver_id': 6844}, {'order_id': 0, 'driver_id': 7700}]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/mdm/venv-torch/lib/python3.5/site-packages/ipykernel_launcher.py:15: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  from ipykernel import kernelapp as app\n",
      "/home/mdm/venv-torch/lib/python3.5/site-packages/ipykernel_launcher.py:19: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "/home/mdm/venv-torch/lib/python3.5/site-packages/ipykernel_launcher.py:22: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    sampledata = [{\"order_id\": 0, \"driver_id\": 36, \"order_driver_distance\": 1126.2238477454885, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.17223539384607, 30.6485633653061], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 375.40794924849615}, {\"order_id\": 0, \"driver_id\": 208, \"order_driver_distance\": 1053.7547898479709, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.1622195095486, 30.662794596354168], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 351.2515966159903}, {\"order_id\": 0, \"driver_id\": 1015, \"order_driver_distance\": 1138.6571871299093, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.160415, 30.656928], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 379.55239570996974}, {\"order_id\": 0, \"driver_id\": 1244, \"order_driver_distance\": 1935.2636459261153, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.16251256808364, 30.673970411552567], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 645.0878819753717}, {\"order_id\": 0, \"driver_id\": 1758, \"order_driver_distance\": 1324.83184991022, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.18570855034723, 30.66096164279514], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 441.61061663673996}, {\"order_id\": 0, \"driver_id\": 4285, \"order_driver_distance\": 969.4801721971353, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.18204833984375, 30.65693332248264], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 323.1600573990451}, {\"order_id\": 0, \"driver_id\": 6133, \"order_driver_distance\": 1228.5477420121815, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.17724745008681, 30.64855984157986], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 409.5159140040605}, {\"order_id\": 0, \"driver_id\": 6844, \"order_driver_distance\": 654.4543412270853, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.17593375824893, 30.66356441259522], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 218.15144707569507}, {\"order_id\": 0, \"driver_id\": 7510, \"order_driver_distance\": 726.0510402552777, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.17337917751736, 30.652246907552083], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 242.0170134184259}, {\"order_id\": 0, \"driver_id\": 7700, \"order_driver_distance\": 821.2972562779313, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.17636528862847, 30.652264539930556], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 273.76575209264377}, {\"order_id\": 0, \"driver_id\": 7899, \"order_driver_distance\": 825.2737189392166, \"order_start_location\": [104.17213000000001, 30.65868], \"order_finish_location\": [104.07704, 30.68109], \"driver_location\": [104.16680640835654, 30.65285006509489], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335000, \"day_of_week\": 2, \"reward_units\": 2.620967741935484, \"pick_up_eta\": 275.09123964640554}, {\"order_id\": 1, \"driver_id\": 208, \"order_driver_distance\": 1439.1476028728016, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.1622195095486, 30.662794596354168], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 479.7158676242672}, {\"order_id\": 1, \"driver_id\": 708, \"order_driver_distance\": 1817.8173728839545, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.17997381743818, 30.686217053660336], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 605.9391242946515}, {\"order_id\": 1, \"driver_id\": 1244, \"order_driver_distance\": 1171.0770650337058, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16251256808364, 30.673970411552567], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 390.3590216779019}, {\"order_id\": 1, \"driver_id\": 1310, \"order_driver_distance\": 1874.2759095443903, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16734130859375, 30.68647216796875], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 624.7586365147968}, {\"order_id\": 1, \"driver_id\": 1684, \"order_driver_distance\": 1895.4101246531948, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16728949012791, 30.68665808893226], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 631.8033748843983}, {\"order_id\": 1, \"driver_id\": 1758, \"order_driver_distance\": 1549.0436728869656, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.18570855034723, 30.66096164279514], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 516.3478909623219}, {\"order_id\": 1, \"driver_id\": 2065, \"order_driver_distance\": 1725.152898021458, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.17261962890625, 30.68612277560764], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 575.0509660071526}, {\"order_id\": 1, \"driver_id\": 2738, \"order_driver_distance\": 1731.5226281126777, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.1697714673517, 30.685776089926435], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 577.1742093708925}, {\"order_id\": 1, \"driver_id\": 4178, \"order_driver_distance\": 1568.392336314236, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16472561451413, 30.682216094561085], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 522.7974454380786}, {\"order_id\": 1, \"driver_id\": 4226, \"order_driver_distance\": 1904.7210297829317, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16724962643582, 30.68673459605026], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 634.9070099276439}, {\"order_id\": 1, \"driver_id\": 4285, \"order_driver_distance\": 1707.8128660040907, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.18204833984375, 30.65693332248264], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 569.2709553346969}, {\"order_id\": 1, \"driver_id\": 6448, \"order_driver_distance\": 1823.2114885345427, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.17995406830457, 30.68627344078293], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 607.7371628448476}, {\"order_id\": 1, \"driver_id\": 6670, \"order_driver_distance\": 1711.2011947497763, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16735106195891, 30.684903441519857], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 570.4003982499254}, {\"order_id\": 1, \"driver_id\": 6739, \"order_driver_distance\": 1716.1573286030734, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.18170716467857, 30.684651419281284], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 572.0524428676912}, {\"order_id\": 1, \"driver_id\": 6844, \"order_driver_distance\": 810.7140809493975, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.17593375824893, 30.66356441259522], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 270.2380269831325}, {\"order_id\": 1, \"driver_id\": 7581, \"order_driver_distance\": 1986.3777212837872, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.15808051215278, 30.68198784722222], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 662.1259070945957}, {\"order_id\": 1, \"driver_id\": 7848, \"order_driver_distance\": 1531.9470260385963, \"order_start_location\": [104.17413, 30.67068], \"order_finish_location\": [104.06004, 30.66109], \"driver_location\": [104.16552323039143, 30.682281290577755], \"timestamp\": 1488330000, \"order_finish_timestamp\": 1488335300, \"day_of_week\": 2, \"reward_units\": 3.4274193548387095, \"pick_up_eta\": 510.6490086795321}]\n",
    "    obs2value(sampledata)\n",
    "    print(sampledata)\n",
    "    a = Agent()\n",
    "    print(a.dispatch(sampledata))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
